<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUserCouponsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_coupons', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('user_id')->unsigned()->comment('用户ID');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('title',100)->comment('券名称');
            $table->bigInteger('type_id')->nullable()->comment('类型ID');
            $table->string('type',20)->comment('券类型');
            $table->char('code',10)->unique()->comment('券码');
            $table->dateTime('valid_at')->comment('生效时间');
            $table->dateTime('due_at')->nullable()->comment('过期时间');
            $table->dateTime('used_at')->nullable()->comment('核销时间');
            $table->string('dealer',255)->nullable()->comment('核销经销商');
            $table->string('dealer_code',20)->nullable()->comment('核销经销商代码');
            $table->integer('org_id')->nullable()->comment('组织ID');
            $table->timestamps();
            $table->index(['type_id','type']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_coupons');
    }
}
